/*
 * @FilePath: /wkw/vite.base.config.ts
 * 介绍:无论哪个环境都会执行的配置项
 */

import vue from "@vitejs/plugin-vue";
import { defineConfig, ConfigEnv } from "vite";
import path from "path";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { VantResolver } from "unplugin-vue-components/resolvers";
import viteCompression from "vite-plugin-compression";
import postcssPresetEnv from "postcss-preset-env";
import VueSetupExtend from "vite-plugin-vue-setup-extend";
import Pages from "vite-plugin-pages";
import { createHtmlPlugin } from "vite-plugin-html";

export default (config: ConfigEnv) =>
  defineConfig({
    base: "./",
    resolve: {
      alias: {
        "@@": path.join(__dirname, "src"),
        "@@router": path.join(__dirname, "src/router/index"),
        "@@defaHttp": path.join(__dirname, "src/utils/defaHttp/index"),
        "@@globalView": path.join(
          __dirname,
          "src/layout/globalView"
        ),
      },
    },
    css: {
      preprocessorOptions: {
        postcss: {
          //查postcss文档
          plugins: [postcssPresetEnv()], //使用postcss的预设环境
        },
      },
    },
    plugins: [
      vue(),
      AutoImport({
        // 自动导入 Vue 相关函数，如：ref, reactive, toRef 等
        imports: ["vue", "vue-i18n", "vue-router", "pinia"],
        dts: "./types/dts/auto-import/imports.d.ts",
      }),
      Components({
        dirs: ["src/components"],
        deep: true,
        extensions: ["vue"],
        dts: "./types/dts/auto-import/components.d.ts",
        resolvers: [VantResolver()],
      }),
      viteCompression({
        verbose: true,
        disable: false,
        threshold: 10240,
        algorithm: "gzip",
        ext: ".gz",
      }),
      // 自动导入路由
      Pages({
        importMode: "async",
        dirs: "src/views",
        exclude: ["**/components/*.vue"], // 排除在外的目录，即不将所有 components 目录下的 .vue 文件生成路由
      }),
      //通过 setup <script name="home"></script>直接指定name名
      VueSetupExtend(),
      //通过 <%= VITE_GLOB_APP_TITLE %> 访问env配置
      createHtmlPlugin(),
    ],
  });
